# coding=utf-8
'''
main function：主要实现把txt中的每行数据写入到excel中,先把两个国际化字符串文件转为txt文本文件，然后一一对照，写入Excel 中
'''
 
#################
#
import xlwt #写入文件
import xlrd #打开excel文件
import re
import sys

reload(sys)
sys.setdefaultencoding('utf8')


zh_fileHandle = open("zh.strings", 'r')
zh_lines = zh_fileHandle.readlines()

en_fileHandle = open("en.strings",'r')
en_lines = en_fileHandle.readlines()

# de_fileHandle = open("es.strings",'r')
# de_lines = de_fileHandle.readlines()


# def de_language(key):
#
#     de_string = ''
#     for line in de_lines:
#         if '=' in line:
#             zh_key = line.split('=')[0].strip()
#             if key == zh_key:
#                 de_string = line.split('=')[1].strip()
#                 break;
#     return de_string;

def zh_language(key):

    zh_string = ''
    for line in zh_lines:
        if '=' in line:
            zh_key = line.split('=')[0].strip()
            if key == zh_key:
                zh_string = line.split('=')[1].strip()
                break;
    return zh_string;



def string_to_excel(excelName):
    # 以下路径改为自己相应的路径

    #新建一个excel文件
    file=xlwt.Workbook(encoding='utf-8',style_compression=0)
    #新建一个sheet
    sheet=file.add_sheet('data')

    index = 0
    for line in en_lines:
        if ('=' in line):
            key = line.split('=')[0].strip()
            value = line.split('=')[1].strip()
            sheet.write(index,0,key)
            sheet.write(index,1,zh_language(key))
            sheet.write(index,2,value)
            # sheet.write(index,3,de_language(key))
            index = index + 1


    file.save(excelName)

    zh_fileHandle.close()
    en_fileHandle.close()
    # de_fileHandle.close()

if __name__ == "__main__":
    # 从字符串文件导出到excel
    string_to_excel('v4.4 iOS翻译(中英).xls')